package com.itheima.tliasmanager.utils;

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.PutObjectRequest;
import com.itheima.tliasmanager.properties.AliOSSProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Component  //IOC注解：创建对象
public class AliYunOSSUtil {


    @Autowired
    private AliOSSProperties aliOSSProperties;

    /*@Value("${aliyun.oss.endpoint}")  //DI注解：设置属性值，从application.yml中根据key获取value
    private String endpoint ;

    @Value("${aliyun.oss.bucketName}")  //DI注解：设置属性值，从application.yml中根据key获取value
    private String bucketName ;

    @Value("${aliyun.oss.region}")  //DI注解：设置属性值，从application.yml中根据key获取value
    private String region;*/


    public  String uploadFile(MultipartFile file) throws com.aliyuncs.exceptions.ClientException, IOException {

        String endpoint = aliOSSProperties.getEndpoint();
        String bucketName = aliOSSProperties.getBucketName();
        String region = aliOSSProperties.getRegion();



        //1.生成一个随机的文件名称
        int point = file.getOriginalFilename().lastIndexOf(".");
        String oldFileType = file.getOriginalFilename().substring(point); // .jpg   .png  .pdf  .word
        String fileName = UUID.randomUUID() + oldFileType;  //xxxxxx.jpg   yyyyyy.png   zzzzzz.pdf

        //开始-----------------------------------------------------------------------------------------------------------------------------------------------------
        //3.将文件转存到指定阿里云OSS服务器
        EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();

        // 创建OSSClient实例。
        // 当OSSClient实例不再使用时，调用shutdown方法以释放资源。
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint("https://"+endpoint)
                .credentialsProvider(credentialsProvider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(region)
                .build();


        // 创建PutObjectRequest对象。                                           上传到oss后文件名称         待上传文件的输入流
        PutObjectRequest putObjectRequest = new PutObjectRequest( bucketName,     fileName,          file.getInputStream()  );

        // 上传字符串。
        ossClient.putObject(putObjectRequest);

        String url = "https://"+bucketName+"."+endpoint+"/"+fileName;
        //结束-----------------------------------------------------------------------------------------------------------------------------------------------------
        return url;
    }
}
